{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9fbd9129-fd7b-4e9a-b52b-bae524fccb88",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "920bb4bd-576e-4e5c-9197-16b358ae3f74",
   "metadata": {},
   "source": [
    "# 随机生成张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ac0e1cf7-e963-4a35-b847-edf4acad67a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.4713, 0.0158]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 产生的张量成正态分布\n",
    "torch.randn(1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f121886b-a92e-49da-8ff6-b7a1b3bfd3fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.1492])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机均匀分布\n",
    "torch.rand(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "887eb0d3-ad24-4eb4-97a1-5b26c96d270a",
   "metadata": {},
   "source": [
    "# numpy 转张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4f88e0dd-a12d-4314-a843-18eb13613c00",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.array([1,2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d35993db-baa4-4622-b42b-aaabf088059f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "74ac2377-b481-4778-a047-3d3121a47b89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这两种方式是以拷贝的方式产生数据，对t1t2的数据修改不会体现在data上\n",
    "t1 = torch.Tensor(data)\n",
    "t2 = torch.tensor(data) # 拷贝数据最有的方式\n",
    "t1[0] = 0\n",
    "t2[0] = 0\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8de0956b-c6f2-4a7f-865d-81e1d7f1203b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 3, 4])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这两种方式是共享数据的方式, 对t3t4的数据修改会体现在data上\n",
    "t3 = torch.as_tensor(data)\n",
    "t4 = torch.from_numpy(data)\n",
    "t3[0] = 0\n",
    "t4[0] = 0\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81a4a12e-8d65-4e21-8e7f-c61f54770fba",
   "metadata": {},
   "source": [
    "# 数组转张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "0f395ee0-514b-436f-8c6e-82025069e3fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2, 3])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = [1,2,3]\n",
    "torch.tensor(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebcd5cf4-24e7-4bcc-869e-b0a6b38f3307",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
